/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.api.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Michal HLavac <hlavki@hlavki.eu>
 */
public class LanguageUtils {

    private static final Logger log = Logger.getLogger(Configuration.class.getName());
    protected static final String PROPERTY_RESOURCE_NAME = "eu/easyedu/dictionary/api/resources/iso-639-2.properties";
    protected static Properties PROPERTIES;

    private LanguageUtils() {
    }

    public static String getLanguage(String isoCode) {
        return getProperty(isoCode, null);
    }

    protected static String getProperty(String name, String defaultValue) {
        if (PROPERTIES == null) {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            InputStream is = cl.getResourceAsStream(PROPERTY_RESOURCE_NAME);
            if (is != null) {
                try {
                    Properties props = new Properties();
                    props.load(is);
                    PROPERTIES = props;
                } catch (IOException e) {
                    log.log(Level.SEVERE, "unable to load resource file: " + PROPERTY_RESOURCE_NAME, e);
                } finally {
                    try {
                        is.close();
                    } catch (Exception e) {
                        log.log(Level.SEVERE, "unable to close resource file: " + PROPERTY_RESOURCE_NAME, e);
                    }
                }
            }
        }
        return PROPERTIES != null ? PROPERTIES.getProperty(name, defaultValue) : defaultValue;
    }
}
